/*     */ package com.zimbra.cs.account.ldap.upgrade;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Entry.EntryType;
/*     */ import com.zimbra.cs.account.ldap.LdapDIT;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.ldap.IAttributes;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapServerType;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions.StopIteratingException;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZSearchScope;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class BUG_57866
/*     */   extends UpgradeOp
/*     */ {
/*     */   private static final String ATTR_NAME = "zimbraIsSystemAccount";
/*     */   private static final String VALUE = "TRUE";
/*     */   
/*     */   void doUpgrade()
/*     */     throws ServiceException
/*     */   {
/*  48 */     ZLdapContext zlc = null;
/*     */     try
/*     */     {
/*  51 */       zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.UPGRADE);
/*     */       try
/*     */       {
/*  54 */         this.printer.println("\n=== Upgrading spam account");
/*  55 */         upgradeSpamAccount(zlc);
/*     */       } catch (ServiceException e) {
/*  57 */         this.printer.printStackTrace("unable to upgrade spam account", e);
/*     */       }
/*     */       try
/*     */       {
/*  61 */         this.printer.println("\n=== Upgrading ham account");
/*  62 */         upgradeHamAccount(zlc);
/*     */       } catch (ServiceException e) {
/*  64 */         this.printer.printStackTrace("unable to upgrade ham account", e);
/*     */       }
/*     */       try
/*     */       {
/*  68 */         this.printer.println("\n=== Upgrading wiki accounts");
/*  69 */         upgradeWikiAccounts(zlc);
/*     */       } catch (ServiceException e) {
/*  71 */         this.printer.printStackTrace("unable to upgrade wiki accounts", e);
/*     */       }
/*     */       try
/*     */       {
/*  75 */         this.printer.println("\n=== Upgrading GAL sync accounts");
/*  76 */         upgradeGalSyncAccounts(zlc);
/*     */       } catch (ServiceException e) {
/*  78 */         this.printer.printStackTrace("unable to upgrade GAL sync accounts", e);
/*     */       }
/*     */       try
/*     */       {
/*  82 */         this.printer.println("\n=== Upgrading quarantine account");
/*  83 */         upgradeQuarantineAccount(zlc);
/*     */       } catch (ServiceException e) {
/*  85 */         this.printer.printStackTrace("unable to upgrade quarantine account", e);
/*     */       }
/*     */     }
/*     */     finally {
/*  89 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   Description getDescription()
/*     */   {
/*  95 */     return new Description(this, new String[] { "zimbraIsSystemAccount" }, new Entry.EntryType[] { Entry.EntryType.ACCOUNT }, null, "TRUE", String.format("set %s to %s on all spam/ham/global and domain wiki/GAL sync/quarantine accounts.", new Object[] { "zimbraIsSystemAccount", "TRUE" }));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void setIsSystemAccount(ZLdapContext zlc, Account acct)
/*     */     throws ServiceException
/*     */   {
/* 106 */     if (acct == null) {
/* 107 */       return;
/*     */     }
/*     */     
/* 110 */     if (!acct.isIsSystemAccount()) {
/* 111 */       Map<String, Object> attrs = new HashMap();
/* 112 */       attrs.put("zimbraIsSystemAccount", "TRUE");
/* 113 */       modifyAttrs(zlc, acct, attrs);
/*     */     }
/*     */   }
/*     */   
/*     */   private void upgradeSpamAccount(ZLdapContext zlc) throws ServiceException {
/* 118 */     String acctName = this.prov.getConfig().getSpamIsSpamAccount();
/* 119 */     if (acctName != null) {
/* 120 */       this.printer.format("Checking spam account %s\n", new Object[] { acctName });
/* 121 */       Account acct = this.prov.get(Key.AccountBy.name, acctName);
/* 122 */       setIsSystemAccount(zlc, acct);
/*     */     }
/*     */   }
/*     */   
/*     */   private void upgradeHamAccount(ZLdapContext zlc) throws ServiceException {
/* 127 */     String acctName = this.prov.getConfig().getSpamIsNotSpamAccount();
/* 128 */     if (acctName != null) {
/* 129 */       this.printer.format("Checking ham account %s\n", new Object[] { acctName });
/* 130 */       Account acct = this.prov.get(Key.AccountBy.name, acctName);
/* 131 */       setIsSystemAccount(zlc, acct);
/*     */     }
/*     */   }
/*     */   
/*     */   private void upgradeWikiAccounts(ZLdapContext zlc) throws ServiceException
/*     */   {
/* 137 */     String acctName = this.prov.getConfig().getNotebookAccount();
/* 138 */     if (acctName != null) {
/* 139 */       this.printer.format("Checking global wiki account %s\n", new Object[] { acctName });
/* 140 */       Account acct = this.prov.get(Key.AccountBy.name, acctName);
/* 141 */       setIsSystemAccount(zlc, acct);
/*     */     }
/*     */     
/*     */ 
/* 145 */     LdapDIT dit = this.prov.getDIT();
/* 146 */     String[] returnAttrs = { "zimbraNotebookAccount" };
/*     */     
/* 148 */     String base = dit.mailBranchBaseDN();
/* 149 */     String query = "(&(objectclass=zimbraDomain)(zimbraNotebookAccount=*))";
/*     */     
/* 151 */     final Set<String> wikiAcctNames = new HashSet();
/* 152 */     SearchLdapOptions.SearchLdapVisitor visitor = new SearchLdapOptions.SearchLdapVisitor(false)
/*     */     {
/*     */       public void visit(String dn, IAttributes ldapAttrs)
/*     */         throws SearchLdapOptions.StopIteratingException
/*     */       {
/*     */         try
/*     */         {
/* 159 */           String acctName = ldapAttrs.getAttrString("zimbraNotebookAccount");
/* 160 */           if (acctName != null) {
/* 161 */             wikiAcctNames.add(acctName);
/*     */           }
/*     */         } catch (ServiceException e) {
/* 164 */           BUG_57866.this.printer.printStackTrace("unsble to search domains for wiki accounts", e);
/*     */         }
/*     */         
/*     */       }
/* 168 */     };
/* 169 */     SearchLdapOptions searchOpts = new SearchLdapOptions(base, getFilter(query), returnAttrs, 0, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
/*     */     
/*     */ 
/*     */ 
/* 173 */     zlc.searchPaged(searchOpts);
/*     */     
/* 175 */     for (String wikiAcctName : wikiAcctNames) {
/* 176 */       this.printer.format("Checking domain wiki account %s\n", new Object[] { wikiAcctName });
/* 177 */       Account acct = this.prov.get(Key.AccountBy.name, wikiAcctName);
/* 178 */       setIsSystemAccount(zlc, acct);
/*     */     }
/*     */   }
/*     */   
/*     */   private void upgradeGalSyncAccounts(ZLdapContext zlc) throws ServiceException {
/* 183 */     LdapDIT dit = this.prov.getDIT();
/* 184 */     String[] returnAttrs = { "zimbraGalAccountId" };
/*     */     
/* 186 */     String base = dit.mailBranchBaseDN();
/* 187 */     String query = "(&(objectclass=zimbraDomain)(zimbraGalAccountId=*))";
/*     */     
/* 189 */     final Set<String> galAcctIds = new HashSet();
/* 190 */     SearchLdapOptions.SearchLdapVisitor visitor = new SearchLdapOptions.SearchLdapVisitor(false)
/*     */     {
/*     */       public void visit(String dn, IAttributes ldapAttrs)
/*     */         throws SearchLdapOptions.StopIteratingException
/*     */       {
/*     */         try
/*     */         {
/* 197 */           String acctId = ldapAttrs.getAttrString("zimbraGalAccountId");
/* 198 */           if (acctId != null) {
/* 199 */             galAcctIds.add(acctId);
/*     */           }
/*     */         } catch (ServiceException e) {
/* 202 */           BUG_57866.this.printer.printStackTrace("unsble to search domains for GAL sync accounts", e);
/*     */         }
/*     */         
/*     */       }
/* 206 */     };
/* 207 */     SearchLdapOptions searchOpts = new SearchLdapOptions(base, getFilter(query), returnAttrs, 0, null, ZSearchScope.SEARCH_SCOPE_SUBTREE, visitor);
/*     */     
/*     */ 
/*     */ 
/* 211 */     zlc.searchPaged(searchOpts);
/*     */     
/* 213 */     for (String galAcctId : galAcctIds) {
/* 214 */       this.printer.format("Checking GAL sync account %s\n", new Object[] { galAcctId });
/* 215 */       Account acct = this.prov.get(Key.AccountBy.id, galAcctId);
/* 216 */       setIsSystemAccount(zlc, acct);
/*     */     }
/*     */   }
/*     */   
/*     */   private void upgradeQuarantineAccount(ZLdapContext zlc) throws ServiceException {
/* 221 */     String acctName = this.prov.getConfig().getAmavisQuarantineAccount();
/* 222 */     if (acctName != null) {
/* 223 */       this.printer.format("Checking quarantine account %s\n", new Object[] { acctName });
/* 224 */       Account acct = this.prov.get(Key.AccountBy.name, acctName);
/* 225 */       setIsSystemAccount(zlc, acct);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/upgrade/BUG_57866.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */